package com.test.rabforspringboot.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class DeadConfig {

    /**
     * 创建死信队列
     * @return
     */
    @Bean
    public Queue deadQueue(){
        Map<String,Object> map=new HashMap<String,Object>();
        //设置死信队列内消息的生存时间（如果队列内的消息一直没有消费者去消费，这条消息能存在多长时间）
        map.put("x-message-ttl",10000);
        //设置超时的消息，将发送给哪个交换机
        map.put("x-dead-letter-exchange","msgExchange");
        map.put("x-dead-letter-routing-key", "msgRoutingKey");
        return new Queue("deadQueue",true,false,false,map);
    }

    /**
     * 创建正常队列（该队列负责接收死信队列中发送过来的消息内容）
     * @return
     */
    @Bean
    public Queue normalQueue(){
        return new Queue("normalQueue");
    }


    /**
     * 创建死信交换机
     * @return
     */
    @Bean
    public DirectExchange deadExchange(){
        return new DirectExchange("deadExchange");
    }

    /**
     * 创建一个正常的交换机
     * @return
     */
    @Bean
    public DirectExchange normalExchange(){
        return new DirectExchange("msgExchange");
    }



    /**
     * 将死信队列和死信交换机进行绑定
     * @return
     */
    @Bean
    public Binding deadBind(){
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("deadRouting");
    }

    /**
     * 将正常交换机和正常队列进行绑定
     * @return
     */
    @Bean
    public Binding normalBind(){
        return BindingBuilder.bind(normalQueue()).to(normalExchange()).with("msgRoutingKey");
    }
    @Bean
    public Binding normalBind1(){
        return BindingBuilder.bind(normalQueue()).to(normalExchange()).with("msgRoutingKey");
    }




}
